package com.lzm.ydpt.module.mall.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.basemvp.basebean.ListPageBean;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.ProductBean;
import com.lzm.ydpt.module.mall.adapter.ProductListAdapter;
import com.lzm.ydpt.module.mall.view.ProdcutItemDecoration;
import com.lzm.ydpt.mvp.contract.ProductListContract;
import com.lzm.ydpt.mvp.presenterimpl.ProductListPresenterImpl;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 智慧商城商品列表
 * 用途：搜索商品列表Fragment
 */
public class ProductListFragment extends BaseFragment<ProductListPresenterImpl> implements ProductListContract.View {

    @BindView(R.id.srl_product_list)
    RefreshLayout srl_product_list;
    @BindView(R.id.rv_product_list)
    RecyclerView rv_product_list;
    @BindView(R.id.ltip_product_list)
    LoadingTip ltip_product_list;
    @BindView(R.id.tv_product_list_count)
    TextView tv_product_list_count;

    private List<ProductBean> mProductList = new ArrayList<>();
    private static ProductListFragment mFragment;
    private String keyword = "";
    private int pageNum = 1;

    private GridLayoutManager gridLayoutManager;// 网格布局
    private ProductListAdapter mListAdapter;

    private long storeId = 0L;
    private int sort_type = 0; // 0：默认，1：销量，2：价格升序，3：价格降序
    private ProdcutItemDecoration addItemDecoration = null;

    /**
     * 根据分类id来回去商品
     *
     * @param storeId 店铺id
     * @return
     */
    public static ProductListFragment newInstance(long storeId) {
        mFragment = new ProductListFragment();
        Bundle bundle = new Bundle();
        bundle.putLong(Constants.INP_SHOP_ID, storeId);
        mFragment.setArguments(bundle);
        return mFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_product_list;
    }

    @Override
    public ProductListPresenterImpl initPresenter() {
        return new ProductListPresenterImpl(this);
    }

    @Override
    protected void initView() {
        storeId = getArguments().getLong(Constants.INP_SHOP_ID, 0);
        initRv();
        initAdapter();
        ltip_product_list.setLoadingTip(LoadingTip.LoadStatus.loading);
        fetchProductList();
    }

    private void initRv() {
        setRefreshLayout(srl_product_list);
        srl_product_list.setEnableLoadMore(false);
        srl_product_list.setRefreshHeader(new RefreshHeaderView(context).getHeaderStyleStaffF4());
        srl_product_list.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                fetchProductList();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                fetchProductList();
            }
        });
    }

    private void initAdapter() {
        addItemDecoration = new ProdcutItemDecoration(40);
        gridLayoutManager = new GridLayoutManager(context, 2);
        rv_product_list.setLayoutManager(gridLayoutManager);
        mListAdapter = new ProductListAdapter(mProductList, context);
        mListAdapter.setItemClickListener(new ProductListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, ProductBean data, int position) {
                Bundle bundle = new Bundle();
                bundle.putLong(Constants.INP_PRODUCT_ID, data.getGoods_id());
//                openActivity(ProductDetailsActivity.class, bundle);
            }
        });
        rv_product_list.addItemDecoration(addItemDecoration);
        rv_product_list.setAdapter(mListAdapter);
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
        this.pageNum = 1;
        startProgressDialog();
        fetchProductList();
    }

    private void fetchProductList() {
        FormBodys.Builder formBodys = new FormBodys.Builder();
        formBodys.add("api_token", "");
        formBodys.add("id", 0);
        formBodys.add("sort_type", sort_type);
        formBodys.add("storeId", storeId);
        if (!TextUtils.isEmpty(keyword)) {
            formBodys.add("keyword", keyword);
        }
        formBodys.add("page", pageNum);
        formBodys.add("per_page", 10);
        formBodys.add("gift_flag", 0);
        mPresenter.onProductList(formBodys.build());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mFragment = null;
    }

    @Override
    public void showMessage(String message, String method) {
        stopProgressDialog();
        showShortToast(message);
    }

    @Override
    public void showErrorView(String msg) {
        showErrorView(msg, ltip_product_list);
        ltip_product_list.setOnReloadListener(view -> {
            ltip_product_list.setLoadingTip(LoadingTip.LoadStatus.loading);
            fetchProductList();
        });
    }

    public void setFilter(int filterData) {
        switch (filterData) {
            case 0:// 默认
                sort_type = 0;
                break;
            case 1:// 销量
                sort_type = 1;
                break;
            case 2:// 价格低到高
                sort_type = 2;
                break;
            case 3:// 价格高到低
                sort_type = 3;
                break;
        }
        pageNum = 1;
        fetchProductList();
    }

    @Override
    public void onProductList(ListPageBean<ProductBean> productList) {
        stopProgressDialog();
        if (pageNum == 1) {
            mProductList.clear();
            srl_product_list.finishRefresh();
        } else {
            srl_product_list.finishLoadMore();
        }
        mProductList.addAll(productList.getList());
        mListAdapter.notifyDataSetChanged();
        srl_product_list.setEnableLoadMore(productList.getList().size() >= 10);
        ltip_product_list.setLoadingTip(mProductList.size() == 0 ? LoadingTip.LoadStatus.empty : LoadingTip.LoadStatus.finish);
        tv_product_list_count.setText("为您找到相关结果约" + productList.getTotal() + "个");
    }
}